package logic

import (
	"al/alrpc/alrpc"
	"al/alrpc/internal/svc"
	"al/cache"
	"al/models/mysql"
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *alrpc.RegisterRequest) (*alrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "sendCode"+in.Mobile).Result()
	if result != in.SendCode {
		return nil, errors.New("短信验证码错误,请重新输入")
	}

	user := mysql.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&user)

	return &alrpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
